package com.yl.ydk.ui.activity

import android.net.wifi.WifiManager
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.yl.ydk.R
import com.yl.ydk.base.BaseActivity
import com.yl.ydk.bean.WifiListBean
import com.yl.ydk.utils.MyWifiManager
import com.yl.ydk.utils.WifiUtils
import com.blankj.utilcode.utils.StringUtils
import com.blankj.utilcode.utils.ToastUtils
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_wifi_connect.*

class WifiConnectActivity : BaseActivity(), View.OnClickListener {

    private var wifiListBean: WifiListBean? = null
    private var mWifiManager: WifiManager? = null
    private var isOpenEye = false

    override fun getLayoutId(): Int = R.layout.activity_wifi_connect

    override fun attachView() {

    }

    override fun detachView() {

    }

    override fun initView() {

        rlBack.setOnClickListener(this)
        rlBuXianShi.setOnClickListener(this)
        btnCancel.setOnClickListener(this)
        val strData = intent.getStringExtra("wifiData")
        wifiListBean = Gson().fromJson<WifiListBean>(strData, WifiListBean::class.java)
        mWifiManager = applicationContext.getSystemService(WIFI_SERVICE) as WifiManager

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rlBack, R.id.btnCancel -> {
                finish()
            }
            R.id.rlBuXianShi -> {
                if (!isOpenEye) {   //密码可见
                    isOpenEye = true
                    etPassword.transformationMethod = HideReturnsTransformationMethod.getInstance()
                    etPassword.setSelection(etPassword.text.length)
                } else { //密码不可见
                    isOpenEye = false
                    etPassword.transformationMethod = PasswordTransformationMethod.getInstance()
                    etPassword.setSelection(etPassword.text.length)
                }
            }

            R.id.btnConnect -> {
                val strPassword = etPassword.text.toString().trim()
                if (StringUtils.isEmpty(strPassword)) {
                    ToastUtils.showLongToastSafe("请输入密码....")
                    return
                }
                if (strPassword.length < 8) {
                    ToastUtils.showLongToastSafe("请输入8位密码....")
                    return
                }
                MyWifiManager.disconnectNetwork(mWifiManager)  //断开当前wifi
                WifiUtils.connect(this, wifiListBean?.SSID!!, strPassword)

//            MyWifiManager.connectWifi(
//                mWifiManager,
//                wifiListBean?.name,
//                etPassword.text.toString().trim(),
//                type
//            ) //连接wifi

            }

        }
    }

}